package org.sample.model.exception;

import org.sample.model.enums.DefaultCodes;
import org.sample.model.enums.ResultCode;

public class APIException extends RuntimeException {

    private int code;

    public APIException(ResultCode code) {
        super(code.getMessage());
        this.code = code.getCode();
    }

    public APIException(ResultCode code, String message) {
        super(code.getMessage() + "[" + message + "]");
        this.code = code.getCode();
    }

    public APIException(ResultCode code, Object message) {
        super(code.getMessage() + "[" + String.valueOf(message) + "]");
        this.code = code.getCode();
    }

    public APIException(Throwable e) {
        super(e);
        this.code = DefaultCodes.UnknownError.code;
    }

    public int getCode() {
        return code;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("{\"");
        sb.append("code\":").append(code);
        sb.append(",\"message\":\"").append(super.getMessage());
        sb.append("\"}");
        return sb.toString();
    }

}